// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require('gulp')
const del = require('del') // 删除目录
const browserSync = require('browser-sync')
const loadPlugins = require('gulp-load-plugins') // 自动加载插件
const plugins = loadPlugins()
const bs = browserSync.create()


const data = {
    menus: [
      {
        name: 'Home',
        icon: 'aperture',
        link: 'index.html'
      },
      {
        name: 'Features',
        link: 'features.html'
      },
      {
        name: 'About',
        link: 'about.html'
      },
      {
        name: 'Contact',
        link: '#',
        children: [
          {
            name: 'Twitter',
            link: 'https://twitter.com/w_zce'
          },
          {
            name: 'About',
            link: 'https://weibo.com/zceme'
          },
          {
            name: 'divider'
          },
          {
            name: 'About',
            link: 'https://github.com/zce'
          }
        ]
      }
    ],
    pkg: require('./package.json'),
    date: new Date()
  }


// 清除dist,.temp 文件
const clear = () => {
    return del(['dist','.temp'])
}
// 清除 .temp 目录
const clearTemp = () => {
    return del(['.temp'])
}


// 样式文件编译
const style = () => {
    return src('src/assets/styles/*.scss', { base: 'src' })
        .pipe(plugins.sass({ outputStyle: 'expanded' }))
        .pipe(dest('.temp'))
        .pipe(bs.reload({ stream: true }))
}

// js编译
const script = () => {
    return src('src/assets/scripts/*.js', { base: 'src' })
        .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
        .pipe(dest('.temp'))
        .pipe(bs.reload({ stream: true }))
}
// 编译页面文件
const page = () => {
    return src('src/*.html', { base: 'src' })
        .pipe(plugins.swig({data, defaults: { cache: false } }))
        .pipe(dest('.temp'))
        .pipe(bs.reload({ stream: true }))
}
// 图片文件的编译
const image = () => {
    return src('src/assets/images/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
}

// 字体文件的压缩
const font = () => {
    return src('src/assets/fonts/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
}
// 公共目录编译
const publics = () => {
    return src('public/**', { base: 'public' })
        .pipe(dest('dist'))
}
// 讲 .temp的所有文件全部进行判断，对js,css,html 压缩
const useref = () => {
    return src('.temp/**', { base: '.temp' })
        .pipe(plugins.useref({ searchPath: ['.temp', '.'] }))
        // 压缩html,js,css
        .pipe(plugins.if(/\.js$/, plugins.uglify()))
        .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
        .pipe(plugins.if(/\.html$/, plugins.htmlmin({
            collapseWhitespace: true,
            minifyCSS: true,
            minifyJS: true
        })))
        .pipe(dest('dist'))
}
// 启动服务
const serve = () => {

    watch('src/*.html', page)
    watch('src/js/*.js', script)
    watch('src/styles/*.scss', style)
    watch(['src/images/**', 'src/fonts/**', 'public/**'], bs.reload)

    bs.init({
        notify: false,
        port: 8080,
        open: false,
        files: 'dist/**',
        server: {
            baseDir: ['dist', 'public', 'src']
        }
    })
}
const compile = parallel(style, script, page)
const build = series(clear, parallel(series(compile, useref), image, font, publics),clearTemp)
const dev = series(build, serve,clearTemp)
module.exports = {
    build,
    dev
}